﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Menus.Model;
using System.Collections.Generic;
using Menus.Model.DB;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;

namespace MenusTestProject
{
    [TestClass]
    public class PlatManager_test
    {
        [TestInitialize]
        public void Initialize()
        {
            Menus.Model.Globals.menusEF = new MenusEntity();
        }

        [TestMethod]
        public void PlatManager_add()
        {
            //initialisation des données du test
            string name = "plat name " + DateTime.Now.ToString();
            List<string> listItem = new List<string>();
            listItem.Add("ingredient1");
            listItem.Add("ingredient2");

            //ajout du plat
            PlatManager manager = new PlatManager();
            manager.add(name, listItem);

            //on test si le plat est bien ajouté
            SqlConnection sqlCnx = new SqlConnection(ConfigurationManager.ConnectionStrings["TestProjectConnectionString"].ConnectionString);
            sqlCnx.Open();
            SqlCommand sqlCmd = new SqlCommand("select * from Plat where libelle = '" + name + "'", sqlCnx);
            SqlDataReader reader = sqlCmd.ExecuteReader();

            //le plat est trouvé?
            var res = reader.HasRows;
            reader.Close();
            Assert.IsTrue(res, "PlatManager_test add ko");

            //on test si on a bien les ingrédients            
            SqlCommand sqlCmd2 = new SqlCommand(String.Format("select count(*) from IngredientPlat where Plat_ID = (select id from plat where libelle = '{0}')", name), sqlCnx);
            SqlDataReader reader2 = sqlCmd2.ExecuteReader();
            reader2.Read();
            var res2 = reader2.GetInt32(0);

            //deux ingrédients?
            Assert.AreEqual(res2, 2, "pb");

            //suppression des éléménts crées
            reader2.Close();
            sqlCmd2.CommandText = String.Format("delete from Ingredientplat where plat_id = (select id from plat where libelle = '{0}')", name);
            sqlCmd2.ExecuteNonQuery();
            sqlCmd2.CommandText = String.Format("delete from plat where libelle = '{0}'", name);
            sqlCmd2.ExecuteNonQuery();

        }
    }
}
